package com.yanqu.road.server.gameplayer.module.xiaomian;

import com.yanqu.road.entity.xiaomian.UserXiaoMianData;
import com.yanqu.road.logic.bussiness.xiaomian.XiaoMianBussiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.utils.LocalDateTimeHelper;

public class XiaoMianModule extends GeneralModule {

    private UserXiaoMianData userXiaoMianData;

    public XiaoMianModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userXiaoMianData = XiaoMianBussiness.getUserXiaoMianData(player.getUserId());
        initData();
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        if(null != userXiaoMianData){
            if(userXiaoMianData.isInsertOption()){
                XiaoMianBussiness.addUserXiaoMianData(userXiaoMianData);
            }else if(userXiaoMianData.isUpdateOption()){
                XiaoMianBussiness.updateUserXiaoMianData(userXiaoMianData);
            }
        }
        return true;
    }

    public UserXiaoMianData getUserXiaoMianData() {
        resetOneDay();
        return userXiaoMianData;
    }

    private void initData(){
        if(null == userXiaoMianData){
            UserXiaoMianData tempData = new UserXiaoMianData();
            tempData.setUserId(player.getUserId());
            tempData.setResetTime(LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000);
            tempData.resetValue();
            tempData.setInsertOption();
            userXiaoMianData = tempData;
        }
    }

    /**
     * 每日重置
     */
    public void resetOneDay(){
        if(null != userXiaoMianData){
            long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
            if(zeroTime > userXiaoMianData.getResetTime()) {
                synchronized (userXiaoMianData) {
                    if(zeroTime > userXiaoMianData.getResetTime()) {
                        userXiaoMianData.resetValue();
                        userXiaoMianData.setResetTime(zeroTime);
                    }
                }
            }
        }
    }

    /**
     * 增加门客升级次数
     * @param times
     */
    public void addPatronsUpgradeTimes(int times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setPatronsUpgradeTimes(userXiaoMianData.getPatronsUpgradeTimes() + times);
            }
        }
    }

    /**
     * 增加美女传唤次数
     * @param times
     */
    public void addBeautyTrystTimes(int times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setBeautyTrystTimes(userXiaoMianData.getBeautyTrystTimes() + times);
            }
        }
    }

    /**
     * 增加子嗣培养次数
     * @param times
     */
    public void addChildTrainTimes(int times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setChildTrainTimes(userXiaoMianData.getChildTrainTimes() + times);
            }
        }
    }

    /**
     * 增加烹饪次数
     * @param times
     */
    public void addInnCookTimes(int times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setInnCookTimes(userXiaoMianData.getInnCookTimes() + times);
            }
        }
    }

    /**
     * 增加联姻次数
     * @param times
     */
    public void addMarriageTimes(int times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setMarriageTimes(userXiaoMianData.getMarriageTimes() + times);
            }
        }
    }

    /**
     * 增加宴会积分
     * @param score
     */
    public void addBanquetScore(long score){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setBanquetScore(userXiaoMianData.getBanquetScore() + score);
            }
        }
    }

    /**
     * 增加医馆接诊数
     * @param count
     */
    public void addClinicReception(int count){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setClinicReception(userXiaoMianData.getClinicReception() + count);
            }
        }
    }

    /**
     * 增加酒肆叫号
     * @param count
     */
    public void addWineShopCallNumber(int count){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setWineShopCallNumber(userXiaoMianData.getWineShopCallNumber() + count);
            }
        }
    }

    /**
     * 增加元宝消耗
     * @param count
     */
    public void addIngotsConsume(long count){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setIngotsConsume(userXiaoMianData.getIngotsConsume() + count);
            }
        }
    }

    /**
    * 描述：增加跨服商会聊天次数
    * 作者：zrq
    * 时间：2022/3/4
    * 参数：

    * 返回值：
    **/
    public void addCrossChatTimes(long times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setCrossChatTimes(userXiaoMianData.getCrossChatTimes() + times);
            }
        }
    }

    public void addGrandChildCount(long count){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setGrandChildCount(userXiaoMianData.getGrandChildCount() + count);
            }
        }
    }

    public void addDrugStorePublicityTimes(long times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setDrugStorePublicityTimes(userXiaoMianData.getDrugStorePublicityTimes() + times);
            }
        }
    }

    public void addWineryMakeTimes(long times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setWineryMakeTimes(userXiaoMianData.getWineryMakeTimes() + times);
            }
        }
    }

    public void addDrugStoreBusinessTimes(long times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setDrugStoreBusinessTimes(userXiaoMianData.getDrugStoreBusinessTimes() + times);
            }
        }
    }

    public void addGiveLikeTimes(long times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setGiveLikeTimes(userXiaoMianData.getGiveLikeTimes() + times);
            }
        }
    }

    public void addPassRoadBossBattleTimes(long times){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setPassRoadBossBattleTimes(userXiaoMianData.getPassRoadBossBattleTimes() + times);
            }
        }
    }

    public void addTradeWarKillPatronsCount(long count){
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setTradeWarKillPatronsCount(userXiaoMianData.getTradeWarKillPatronsCount() + count);
            }
        }
    }

    public void addJoinWaSheTimes(long times) {
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setJoinWaSheTimes(userXiaoMianData.getJoinWaSheTimes() + times);
            }
        }
    }

    public void addUnionBargainTimes(long times) {
        if(null != userXiaoMianData){
            synchronized (userXiaoMianData){
                resetOneDay();
                userXiaoMianData.setUnionBargainTimes(userXiaoMianData.getUnionBargainTimes() + times);
            }
        }
    }
}
